<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="nickName" class="item">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="workName" class="item">
        <el-input
          v-model="queryParams.workName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
  
      <el-form-item label="风险等级" prop="riskGrade" class="item">
        <el-select v-model="queryParams.riskGrade" placeholder="请选择风险等级" clearable size="small">
          <el-option
            v-for="dict in riskGradeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属科室" prop="deptName" class="item">
        <treeselect v-model="queryParams.deptId" clearable :options="deptOptions" :loadOptions="null" :disable-branch-nodes="false" :show-count="true" placeholder="请选择归属部门"  style="width:200px;" />
      </el-form-item>
      <el-form-item label="任务来源" prop="workSource">
        <el-select v-model="queryParams.workSource" placeholder="请选择任务来源" clearable size="small">
          <el-option
            v-for="dict in workSourceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否超时" prop="isTimeOut" class="item">
        <el-input
          v-model="queryParams.isTimeOut"
          placeholder="请输入是否超时"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="任务时限" class="item">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="任务状态" prop="state" class="item">
        <el-select v-model="queryParams.state" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:process:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange" style="width: 100%"
    height="600">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="nickName" />
      <el-table-column label="任务名称" align="center" prop="workName" />
      <el-table-column label="工作内容" align="center" prop="workContent" />
      <el-table-column label="风险等级" align="center" prop="riskGrade" />
      <el-table-column label="组织机构" align="center" prop="deptName" />
      <el-table-column label="任务来源" align="center" prop="workSource" />
      <el-table-column label="工作时长" align="center" prop="workHours" />
      <el-table-column label="文件时长" align="center" prop="fileHours" />
      <el-table-column label="文本工时" align="center" prop="textHours" />
      <el-table-column label="是否超时" align="center" prop="isTimeOut" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
       <el-table-column label="任务状态" align="center" prop="state" />
      <el-table-column label="实际完成时间" align="center" prop="realFinishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realFinishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
     
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listProcess, getProcess, delProcess, addProcess, updateProcess, exportProcess } from "@/api/work/export";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Process",
  components: { Treeselect },
  data() {
    return {
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 风险等级字典
      riskGradeOptions: [],
      // 任务来源字典
      workSourceOptions: [],
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: [],
      // 任务导出表格数据
      processList: [],
      // 任务状态字典
      stateOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        workName: null, 
        workContent: null,
        riskGrade: null,
        deptName: null,
        workSource: null,
        workGroup: null,
        workHours: null,
        fileHours: null,
        textHours: null,
        isTimeOut: null,
        startTime: null,
        finishTime: null,
        realFinishTime: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("work_risk_grade").then(response => {
      this.riskGradeOptions = response.data;
    });
    this.getDicts("work_file_source").then(response => {
      this.workSourceOptions = response.data;
    });
    this.getDicts("work_file_state").then(response => {
      this.stateOptions = response.data;
    });
    this
  },
  methods: {
    /** 查询任务导出列表 */
    getList() {
      this.loading = true;
      listProcess(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        nickName: null,
        workName: null,
        workContent: null,
        riskGrade: null,
        deptName: null,
        workSource: null,
        workGroup: null,
        workHours: null,
        fileHours: null,
        textHours: null,
        isTimeOut: null,
        startTime: null,
        finishTime: null,
        realFinishTime: null,
        state: null
      };
      this.resetForm("form");

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange= [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有任务导出数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProcess(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
